var wxRequest = require('../../utils/wxRequest')
var Common = require('../../utils/common');
const app = getApp();
Page({
  setdata: app.setdata,
  /**
   * 页面的初始数据
   */
  data: {
    mrRoomsList: [],
    identityList: [0,1,2],
    identityNameList: ["业主", "家属","租客"],
    identityName: "请选择身份",
    identityId:-1,

    romName:"请选择房号",

    communityRoomNameList: [],
    communityRoomIdList: [],
    communityRoomName: "请选择",
    communityRoomId: 0,

    namefocus: false,
    namefocus: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.init(this)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getHouseList();
  },
  getHouseList: function () {
    var _that = this;
    var url = app.globalData.domain + "/client/community/getAllBindRoom";
    var parm = {
      token: wx.getStorageSync("token"),
      idCommunity: wx.getStorageSync("idCommunity")
    }
    wxRequest.postRequest(url, parm).then(res => {
      console.log("所有绑定房屋=>", res);
      console.log(res.data.mrRoom)
      
      var mrRoomsList = res.data.notMrRooms;
      if (res.data.mrRoom != undefined) {
        mrRoomsList = mrRoomsList.concat(res.data.mrRoom)
      }

      this.setData({
        mrRoomsList: mrRoomsList,
      });

      console.log(this.data.mrRoomsList)

      var communityRoomNameList = [];
      var communityRoomIdList = [];
      if (_that.data.mrRoomsList.length > 0) {
        for (var index in _that.data.mrRoomsList) {
          communityRoomNameList[index] = _that.data.mrRoomsList[index].nameCommunity + _that.data.mrRoomsList[index].nameArea + _that.data.mrRoomsList[index].nameFloor + _that.data.mrRoomsList[index].nameRoom;
          communityRoomIdList[index] = _that.data.mrRoomsList[index].idRoom;
        }
        _that.setData({
          communityRoomNameList: communityRoomNameList,
          communityRoomIdList: communityRoomIdList
        });
      }

    }, error => {
      Common.showToast(error);
    });
  },
  //选择身份
  identityListChange: function (event) {
    var that = this;
    console.log("communityListValue", event);
    that.setData({
      identityName: that.data.identityNameList[event.detail.value],
      identityId: that.data.identityList[event.detail.value]
    });
  },
  //选择房间
  communityRoomListChange: function (event){
    var that = this;
    console.log("communityListValue", event);
    if (that.data.communityRoomNameList.length > 0 && that.data.communityRoomIdList.length > 0){
      that.setData({
        communityRoomName: that.data.communityRoomNameList[event.detail.value],
        communityRoomId: that.data.communityRoomIdList[event.detail.value]
      });
    }else{
      Common.showToast("您还没有选择房间");
    }
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  formSubmit: function (e) {
    var that = this;
    console.log(e);

    if (app.checkNull(e.detail.value.name)){
        Common.showToast("姓名不能为空");
        return false;
    }
    if (app.checkPhone(e.detail.value.phone)) {
      Common.showToast("手机格式不正确");
      return false;
    }
    if (that.data.communityRoomId == 0) {
      Common.showToast("请选择房间号");
      return false;
    }
    if (that.data.identityId == -1) {
      Common.showToast("请选择身份");
      return false;
    }
    Common.showLoading("正在生成二维码...");
    var url = app.globalData.domain + "/client/user/inviteUser";
    var parm = {
      token: wx.getStorageSync("token"),
      name: e.detail.value.name,
      phone: e.detail.value.phone,
      idRoom: that.data.communityRoomId,
      sf: that.data.identityId
    }
    var invireFamilyInfo = null;
    wxRequest.postRequest(url, parm, wx.getStorageSync("token")).then(res => {
      console.log("邀请结果=>", res);
      invireFamilyInfo = {
        name: e.detail.value.name,
        phone: e.detail.value.phone,
        room: that.data.communityRoomName,
        sf: that.data.identityId,
        qr: res.data
      }
      wx.setStorageSync("invireFamilyInfo", invireFamilyInfo);
      Common.hideLoading();
      wx.redirectTo({
        url: '/pages/invite_info/invite_info?id=0'
      })
    }, error => {
      wx.setStorageSync("invireFamilyInfo", invireFamilyInfo);
      Common.hideLoading();
      Common.showToast(error);
    });
  },
})